package com.cet.designapplication.decorator;

import com.cet.designapplication.decorator.strategy.ScoreAndRegBagSendStrategy;
import com.cet.designapplication.decorator.strategy.factory.ScoreAndRegBagSendHandlerFactory;
import com.cet.designapplication.pojo.Order;
import com.cet.designapplication.repo.ProductsRepository;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;
import java.math.BigDecimal;

/**
 * @author NSOC
 */

@Component
public class OrderServiceDecorator extends AbstractOrderServiceDecorator{

    @Autowired
    private ScoreAndRegBagSendHandlerFactory scoreAndRegBagSendHandlerFactory;

    @Override
    protected void updateScoreAndSendRedPaper(String productId, int serviceLevel, BigDecimal price) {
        ScoreAndRegBagSendStrategy handler = scoreAndRegBagSendHandlerFactory.getHandler(serviceLevel);
        handler.sendScoreAndRegBag(productId, price);
    }

    public Order decoratorPay(String orderId, int serverLevel, BigDecimal price) {

        Order order = super.pay(orderId);
        try {
            updateScoreAndSendRedPaper(order.getProductId(), serverLevel, price);
        } catch (Exception e) {
            // 重试机制,积分更新不能影响支付核心流程
        }
        return order;
    }
}
